package cn.darksoul3.thinking.in.spring.ioc.overview.dependency.injection;

import cn.darksoul3.thinking.in.spring.ioc.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;


public class DependencyInjectionDemo {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INFO/dependency-injection-context.xml");
        //依赖来源1: 自定义Bean
        UserRepository userRepository = beanFactory.getBean("userRepository", UserRepository.class);
        System.out.println(userRepository.getUsers());
        //依赖注入 (内建依赖)
        //org.springframework.beans.factory.support.DefaultListableBeanFactory@22f71333:
        // defining beans [user,objectFactory,superUser,userRepository]; root of factory hierarchy
        //依赖来源2: 内建依赖
        System.out.println(userRepository.getBeanFactory());
        //注入的beanFactory和当前的beanFactory并不是同一个
        System.out.println(userRepository.getBeanFactory() == beanFactory);

        //依赖注入上下文中的beanFactory和当前的beanFactory是同一个
        ObjectFactory<ApplicationContext> objectFactory = userRepository.getObjectFactory();
        System.out.println(objectFactory.getObject() == beanFactory);

        //依赖查找
        //No qualifying bean of type 'org.springframework.beans.factory.BeanFactory' available
        //依赖查找和依赖注入并非同源
        //System.out.println(beanFactory.getBean(BeanFactory.class));

        //依赖来源3: 容器内建 Bean
        //容器内建 Bean
        Environment environment = beanFactory.getBean(Environment.class);
        System.out.println("environment:" + environment);



    }


}
